package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import hcn.base.BaseModuleConfig;
import org.hibernate.Query;
import org.hibernate.StatelessSession;

import java.util.List;

public abstract class BaseModuleConfigDAO extends HibernateSupportDelegateDAO<BaseModuleConfig> {
    public BaseModuleConfigDAO() {
        this.setKeyField("id");
    }

    @DAOMethod(sql = "from BaseModuleConfig where moduleId=:moduleId and objectId=:objectId")
    public abstract BaseModuleConfig getModuleConfigByModuleId(@DAOParam("moduleId") String moduleId,@DAOParam("objectId") String objectId);

    @DAOMethod(sql = "from BaseModuleConfig where objectId=:objectId and level=:level")
    public abstract List<BaseModuleConfig> queryAllModuleConfig(@DAOParam("objectId") String objectId,@DAOParam("level") String level);

    @DAOMethod(sql = "from BaseModuleConfig where level=:level")
    public abstract List<BaseModuleConfig> queryAllCloudModuleConfig(@DAOParam("level") String level);

    @DAOMethod(sql = "from BaseModuleConfig where objectId=:objectId and moduleId=:moduleId and level=:level")
    public abstract List<BaseModuleConfig> queryModuleConfig(@DAOParam("objectId") String objectId,@DAOParam("moduleId") String moduleId,@DAOParam("level") String level);

    @DAOMethod(sql = "from BaseModuleConfig where moduleId=:moduleId and level=:level")
    public abstract List<BaseModuleConfig> queryCloudModuleConfig(@DAOParam("moduleId") String moduleId,@DAOParam("level") String level);

    @DAOMethod(sql = "from BaseModuleConfig where moduleId=:moduleId and objectId=:objectId ")
    public abstract BaseModuleConfig getModuleConfigByModuleIdAndObjectId(@DAOParam("moduleId") String moduleId,@DAOParam("objectId") String objectId);

    /**
     * @description: 查询功能配置
     * @author: chenJij@bsoft.com.cn
     * @date: 2019/11/19 18:36
     */
    public List<BaseModuleConfig> queryModuleConfigList(final String cloudId, final String level, final String moduleId, final String objectId){
        HibernateStatelessResultAction<List<BaseModuleConfig>> action = new AbstractHibernateStatelessResultAction<List<BaseModuleConfig>>(){
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append(" SELECT a FROM BaseModuleConfig a  left join  Orgcloudmem b  on  a.objectId = b.orgId ");
                hsql.append(" WHERE  1=1 ");
                if(CommonUtils.isNotEmpty(cloudId)){
                    hsql.append(" AND b.cloudId =:cloudId ");
                }
                if(CommonUtils.isNotEmpty(level)){
                    hsql.append(" AND a.level =:level ");
                }
                if(CommonUtils.isNotEmpty(moduleId)){
                    hsql.append(" AND a.moduleId=:moduleId ");
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    hsql.append(" AND a.objectId=:objectId ");
                }
                Query query = ss.createQuery(hsql.toString());
                if(CommonUtils.isNotEmpty(cloudId)){
                    query.setParameter("cloudId",cloudId);
                }
                if(CommonUtils.isNotEmpty(level)){
                    query.setParameter("level",level);
                }
                if(CommonUtils.isNotEmpty(moduleId)){
                    query.setParameter("moduleId",moduleId);
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    query.setParameter("objectId",objectId);
                }
//                query = (SQLQuery) query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
//                List<Map> dbList = query.list();

                List<BaseModuleConfig> list = (List<BaseModuleConfig>) query.list();
                setResult(list);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        List<BaseModuleConfig> list = action.getResult();
        return list;
    }

    /**
     * @Describe: 查询功能配置
     * @Param: [cloudId, level, moduleId, objectId]
     * @Return: java.util.List<hcn.base.BaseModuleConfig>
     * @Author: zhengcb10708
     * @Date: 2021/5/25 17:16
     */
    public List<BaseModuleConfig> queryModuleConfigByParams(final String level, final String moduleId, final String objectId){
        HibernateStatelessResultAction<List<BaseModuleConfig>> action = new AbstractHibernateStatelessResultAction<List<BaseModuleConfig>>(){
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hql = new StringBuffer();
                hql.append(" FROM BaseModuleConfig a ");
                hql.append(" WHERE  1=1 ");

                if(CommonUtils.isNotEmpty(level)){
                    hql.append(" AND a.level =:level ");
                }
                if(CommonUtils.isNotEmpty(moduleId)){
                    hql.append(" AND a.moduleId=:moduleId ");
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    hql.append(" AND a.objectId=:objectId ");
                }
                Query query = ss.createQuery(hql.toString());

                if(CommonUtils.isNotEmpty(level)){
                    query.setParameter("level",level);
                }
                if(CommonUtils.isNotEmpty(moduleId)){
                    query.setParameter("moduleId",moduleId);
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    query.setParameter("objectId",objectId);
                }
                List<BaseModuleConfig> list = (List<BaseModuleConfig>)query.list();
                setResult(list);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}